<?php

/**
 * 多语言类
 * User: Administrator
 * Date: 2015/6/25
 * Time: 16:20
 */
class LibLanguage
{
    /**
     * 中文
     */
    const CHINA = "ZN";

    /**
     * 英文
     */
    const ENGLISH = "EN";

    static $path = [];

    static $language = [];

    /**
     * 设置语境
     * @param string $language
     * @param string $app
     * @return bool
     */
    static public function set($language = self::CHINA, $app = "")
    {
        define("CURRENT_LANGUAGE_TYPE", $language);
        if ($app) {
            define("CURRENT_LANGUAGE_APPLICATION", $app);
        }
        return true;
    }

    /**
     * 根据key获取对应语言
     * @param $key
     * @return string
     */
    static public function  get($key)
    {
        $path = self::getLPath();
        $app = self::getApp();
        self::$language[$app] = isset(self::$language[$app]) ? self::$language[$app] : include_once $path;
        $value = isset(self::$language[$app][$key]) ? self::$language[$app][$key] : "";
        return $value;
    }

    /**
     * 得到配置文件的地址
     * @return string
     */
    static public function getLPath()
    {
        $app = self::getApp();
        if (isset(self::$path[$app])) {
            return self::$path[$app];
        }
        $language = defined("CURRENT_LANGUAGE_TYPE") && CURRENT_LANGUAGE_TYPE ? CURRENT_LANGUAGE_TYPE : self::CHINA;
        $file = "L_" . $language . ".php";
        $dir = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . "Application" . DIRECTORY_SEPARATOR . $app . DIRECTORY_SEPARATOR . "Language" . DIRECTORY_SEPARATOR;
        self::$path[$app] = $dir . $file;
        if (!file_exists(self::$path[$app])) {
            \LibLog::custom("{$app} Language Config File Not Exists , dir:" . self::$path[$app]);
        }
        return self::$path[$app];
    }

    /**
     * 得到语言的项目配置
     * @return string
     */
    static public function getApp()
    {
        return defined("CURRENT_LANGUAGE_APPLICATION") && CURRENT_LANGUAGE_APPLICATION ? CURRENT_LANGUAGE_APPLICATION : "Manager";
    }
} 